<template>
  <div class="app-container">
     <!-- <div class="detail-title">自定义方法模块</div> -->
    <el-row>
      <el-col :span="12">
        <div class="detail-title-form">
          <el-form ref="elform" :model="form" label-width="100px" :rules="elrules">
            <el-form-item label="评估方法：" prop="dlltype">
              <el-select v-model="form.dlltype" placeholder="评估方法" @change="handleChange">
                <el-option :label="item.label" :value="item.value" v-for="item in moduleClassSelectList"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
    <div style="height:calc(110%)"  v-if = "form.dlltype=='dvs1608_25'">
      <dvs160825 :data="form"  ref="dvs160825"/>
    </div>
    <div style="height:calc(110%)" v-else-if = "form.dlltype=='dvs1608_26'">
      <dvs160826 :data="form"  ref="dvs1608_26"/>
    </div>

  </div>
</template>

<script>
  import service from "@/api/material";
  import Dvs160825 from "./dvs160825";
  import Dvs160826 from "./dvs160826";
  import SnChart from "./snChart";
  export default {
    name: "Dvs1608",
    components: {
      SnChart,
      Dvs160825,
      Dvs160826,
    },
    props: {
      // 值
      value: [String, Object, Array],
      // 数量限制
      limit: {
        type: Number,
        default: 1,
      },
      // 大小限制(MB)
      fileSize: {
        type: Number,
        default: 5,
      },
      // 文件类型, 例如['png', 'jpg', 'jpeg']
      fileType: {
        type: Array,
        default: () => ["xlsx", "xls"],
      },
    },
    data() {
      return {
        elrules:{
          dlltype: [{
            required: true,
            message: '模块分类不能为空',
            trigger: 'change'
          }]
        },
        rules1:{
          m1: [{
            required: true,
            message: 'P-S-N第一段折线斜率负倒数不能为空',
            trigger: 'blur'
          }],
          m2: [{
            required: true,
            message: 'P-S-N第二段折线斜率负倒数不能为空',
            trigger: 'blur'
          }],
          m3: [{
            required: true,
            message: 'P-S-N第二段折线斜率负倒数不能为空',
            trigger: 'blur'
          }]
        },
        rules2:{
          ktd: [{
            required: true,
            message: '温度系数不能为空',
            trigger: 'blur'
          }],
          kwk: [{
            required: true,
            message: '设计系数不能为空',
            trigger: 'blur'
          }],
          jf: [{
            required: true,
            message: '材料安全系数不能为空',
            trigger: 'change'
          }],
          jg: [{
            required: true,
            message: '铸造安全系数不能为空',
            trigger: 'change'
          }],
          js: [{
            required: true,
            message: '载荷安全系数不能为空',
            trigger: 'blur'
          }]
        },
        rules3:{
          rst: [{
            required: true,
            message: '残余应力类型不能为空',
            trigger: 'change'
          }]
        },
        form:{
          dlltype: 'dvs1608_25'
        },
        form1:{},
        item: [1],
        logText: '',
        moduleClassSelectList: [
          {value: 'dvs1608_25', label: '局部应力法',},
          {value: 'dvs1608_26', label: '名义应力法',},
        ],
        JGSelectList: [
          {value: 0, label: '是',},
          {value: 1, label: '否',},
        ],
        IMSelectList: [
          {value: 1, label: '细晶结构钢',},
          {value: 2, label: '不锈钢',},
          {value: 3, label: '淬火钢',},
          {value: 4, label: '锻钢',},
          {value: 5, label: '其他钢',},
          {value: 6, label: '一般用途铸钢和热处理铸钢',},
          {value: 7, label: '球墨铸铁',},
          {value: 8, label: '可锻铸铁',},
          {value: 9, label: '片状石墨铸铁',},
          {value: 10, label: '挤压铝合金',},
          {value: 11, label: '变形铝合金',},
        ],

        JFSelectList:[
          {
            value: 0,
            label: '非常规监测',
            children: [{
              value: 1.5,
              label: '严重'
            }, {
              value: 1.4,
              label: '一般'
            }, {
              value: 1.3,
              label: '可接受'
            }]
          }, {
            value: 1,
            label: '常规监测',
            children: [{
              value: 1.35,
              label: '严重'
            }, {
              value: 1.25,
              label: '一般'
            }, {
              value: 1.2,
              label: '可接受'
            }]
          }
        ],
        correctionMethodSelectList: [
          {value: 1, label: 'FKM',},
          {value: 2, label: 'Goodman',},
          {value: 3, label: 'Geber',},
          {value: 4, label: 'Vonmiss-Hencky',},
          {value: 5, label: 'IMR',},
        ],
        SMINSelectList: [
          {value: 1, label: '高',},
          {value: 2, label: '中',},
          {value: 3, label: '小',},
        ],
        logModel: false,
        wdModel: false,
        dllModel: false,
        heightTestData: '',
        logContentHeight: '',
        number: 0,
        uploadList: [],
        fileList: [],
        dl: '',
        ts: '',
        loglineChartData: {},
        showResult: false,
        showSN: false,
        selectedLabel: ""
      };
    },
    created() {
      this.logContentHeight = window.innerHeight - 570
    },
    watch: {
      value: {
        handler(val) {
          if (val) {
            let temp = 1;
            // 首先将值转为数组
            const list = Array.isArray(val) ? val : this.value.split(',');
            // 然后将数组转为对象数组
            this.fileList = list.map(item => {
              if (typeof item === "string") {
                item = { name: item, url: item };
              }
              item.uid = item.uid || new Date().getTime() + temp++;
              return item;
            });
          } else {
            this.fileList = [];
            return [];
          }
        },
        deep: true,
        immediate: true
      }
    },
    mounted() {
      this.$nextTick(()=>{
        this.heightTestData = window.innerHeight - 300
      })
      this.showResult = false
      this.showSN = false
    },
    methods: {
      listClass(index) {
      },
      goDetail(res) {
        this.$router.push({path: '/material/detail'})
      },
      handleChange(value) {
        this.selectedLabel = this.moduleClassSelectList.find(option => option.value === value).label;
        this.form = {}
        this.$set(this.form, "dlltype", value)
        this.showResult = false
        this.showSN = false
        this.logText = null
        this.loglineChartData = {}
        this.abk = null
        this.fileList = []
      },
      handleJFChange(e){
        console.log(e)
      },
      showLogModel() {
        this.logModel = true
      },
      showWdModel() {
        this.wdModel = true
      },
      showKwkModel() {
        if(this.form.dlltype==null || this.form.dlltype==undefined || this.form.dlltype==""){
          this.$modal.msgError(`模块分类不能为空!`);
          return false;
        }
        if(this.form.materialtype==null || this.form.materialtype==undefined || this.form.materialtype==""){
          this.$modal.msgError(`材料类别IM不能为空!`);
          return false;
        }
        if(this.form.dlltype == 'FKMSNWDLL'  || this.form.dlltype == 'FKMTNWDLL'){
          if(this.form.rm==null || this.form.rm==undefined || this.form.rm==""){
            this.$modal.msgError(`材料抗拉强度Rm不能为空!`);
            return false;
          }
        }else if(this.form.dlltype == 'FKMSWDLL' || this.form.dlltype == 'FKMTWDLL'){
          if(this.form.m1==null || this.form.m1==undefined || this.form.m1==""){
            this.$modal.msgError(`PSN第一折线段疲劳强度指数m1不能为空!`);
            return false;
          }
          if(this.form.nc==null || this.form.nc==undefined || this.form.nc==""){
            this.$modal.msgError(`PSN的FAT疲劳强度幅值nc不能为空!`);
            return false;
          }
          if(this.form.nd==null || this.form.nd==undefined || this.form.nd==""){
            this.$modal.msgError(`PSN两折线段拐点对应疲劳寿命nd不能为空!`);
            return false;
          }
        }
        this.dllModel = true
      },
      // 上传前校检格式和大小
      handleBeforeUpload(file) {
        // 校检文件类型
        if (this.fileType) {
          const fileName = file.name.split('.');
          const fileExt = fileName[fileName.length - 1];
          const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
          if (!isTypeOk) {
            this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
            return false;
          }
        }
        // 校检文件大小
        if (this.fileSize) {
          const isLt = file.size / 1024 / 1024 < this.fileSize;
          if (!isLt) {
            this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
            return false;
          }
        }
        // this.$modal.loading("正在上传文件，请稍候...");
        // this.number++;
        this.fileList = [file]
        return true;
      },
      // 文件个数超出
      handleExceed() {
        this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
      },
      // 获取文件名称
      getFileName(name) {
        // 如果是url那么取最后的名字 如果不是直接返回
        if (name.lastIndexOf("/") > -1) {
          return name.slice(name.lastIndexOf("/") + 1);
        } else {
          return name;
        }
      },
      // 对象转成指定字符串分隔
      listToString(list, separator) {
        let strs = "";
        separator = separator || ",";
        for (let i in list) {
          strs += list[i].url + separator;
        }
        return strs != '' ? strs.substr(0, strs.length - 1) : '';
      },
      computeParamHD1(form){
        var that = this
        this.$refs.wform.validate(valid => {
          if (!valid) return
          let formData = new FormData()
          formData.append('wd',form.wd1)
          formData.append('materialtype',form.materialtype1)
          service.computeParamWD(formData).then(res => {
            this.$set(this.form1, "ktb", res.data.value)
            this.wdModel1 = false
          }).catch((err)=>{
            if(err.code == 500){
              that.$modal.msgError(err.mas);
            }
          })
        })
      },
      computeParamWD1(form){
        var that = this
        this.$refs.wform.validate(valid => {
          if (!valid) return
          let formData = new FormData()
          formData.append('wd',form.wd)
          formData.append('materialtype',form.materialtype)
          service.computeParamWD(formData).then(res => {
            this.$set(this.form1, "kt", res.data.value)
            this.wdModel = false
          }).catch((err)=>{
            if(err.code == 500){
              that.$modal.msgError(err.mas);
            }
          })
        })
      },
      computeStructureParamDesign(form){
        this.$refs.kform.validate(valid => {
          if (!valid) return
          let formData = new FormData()
          formData.append('dlltype',form.dlltype)
          formData.append('materialtype',form.materialtype)
          if(form.dlltype == 'FKMSNWDLL'){
            formData.append('gsigamma',form.gsigamma)
            formData.append('rm',form.rm)
            formData.append('rz',form.rz)
            formData.append('kt',form.kt)
            formData.append('kv',form.kv)
            formData.append('ks',form.ks)
            formData.append('knle',form.knle)
          }else if(form.dlltype == 'FKMSWDLL'){
            formData.append('m1',form.m1)
            formData.append('nc',form.nc)
            formData.append('nd',form.nd)
            formData.append('t',form.t)
            formData.append('kv',form.kv)
            formData.append('knle',form.knle)
          }else if(form.dlltype == 'FKMTNWDLL'){
            formData.append('gsigamma',form.gsigamma)
            formData.append('rm',form.rm)
            formData.append('rz',form.rz)
            formData.append('kt',form.kt)
            formData.append('kv',form.kv)
            formData.append('ks',form.ks)
          }else if(form.dlltype == 'FKMTWDLL'){
            formData.append('m1',form.m1)
            formData.append('nc',form.nc)
            formData.append('nd',form.nd)
            formData.append('t',form.t)
            formData.append('kv',form.kv)
          }
          service.computeStructureParamDesign(formData).then(res => {
            this.$set(this.form, "kwk", res.data.value)
            this.dllModel = false
          }).catch((err)=>{
            if(err.code == 500){
              that.$modal.msgError(err.mas);
            }
          })
        })
      },
      count(form){
        this.$refs.elform.validate(valid => {
          if (!valid) return
          this.$refs.mform.validate(valid2 => {
            if (!valid2) return
            this.$refs.jform.validate(valid3 => {
              if (!valid3) return
              this.$refs.fform.validate(valid3 => {
                if (!valid3) return
                console.log(form)
                console.log(this.$refs)
                if(this.fileList.length==0){
                  this.$modal.msgError(`请上传服役载荷文件!`);
                  return false
                }
                let file = this.fileList[0]
                let formData = new FormData()
                formData.append('file',file)
                formData.append('dlltype',form.dlltype)
                formData.append('materialtype',form.materialtype)
                formData.append('rm',form.rm)
                formData.append('m1',form.m1)
                formData.append('m2',form.m2)
                formData.append('nc',form.nc)
                formData.append('nd',form.nd)
                formData.append('sc',form.sc)
                formData.append('kwk',form.kwk)
                formData.append('ktd',form.ktd)
                formData.append('jf',form.jf[1])
                formData.append('jg',form.jg)
                formData.append('js',form.js)
                formData.append('rst',form.rst)
                service.fkmMoudleForecast(formData).then(res => {
                  this.showResult = true
                  this.showSN = true
                  this.logText = res.data.contents
                  // this.loglineChartData.xData = "寿命"
                  // this.loglineChartData.yData = "应力"
                  this.loglineChartData.title = this.selectedLabel+"SN图"
                  this.loglineChartData.ymin = res.data.ymin
                  this.loglineChartData.xmin = res.data.xmin
                  this.loglineChartData.ymax = res.data.ymax
                  this.loglineChartData.xmax = res.data.xmax
                  this.loglineChartData.data = res.data.data
                  this.abk = res.data.abk.toExponential()

                  this.$nextTick(() => {
                    this.$refs.snchart[0].initChart()
                  });
                }).catch((err)=>{
                  if(err.code == 500){
                    that.$modal.msgError(err.mas);
                  }

                })
              })
            })
          })
        })
      }
    }
  };
</script>

<style scoped lang="scss">
  .app-container {
    display: block;
    padding: 5px;
    height: calc(100% - 45px);
  }
  .container-bottom{
    height: calc(100% - 295px);
    overflow: hidden;
  }

  .detail-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-weight: bold;
    font-size: 20px;

    &:before, &:after {
      content: '';
      display: inline-block;
      width: 37%;
      height: 4px;
      border-bottom: 1px solid #BAD7EC;
      border-top: 1px solid #BAD7EC;
    }
  }

  .detail-title-form {
    width: 450px;
    margin: 10px auto;
    ::v-deep .el-form-item{
      margin-bottom: 15px;
    }
    ::v-deep .el-form-item__label {
      color: #084EBC;
      font-weight: normal;
    }
  }

  .container {
    width: 100%;
    display: flex;
    justify-content: space-around;
  }

  .container-warp {
    width: 100%;
    display: inline-block;
    border: 1px solid #ddd;
    background-color: #fff;
    box-shadow: 0 0 5px #eeeeee;
  }

  .container-right {
    .container-warp + .container-warp {
      margin-top: 10px;
    }
  }

  .container-left {
    width: 49%;
  }

  .container-right {
    width: 49%;
  }

  .detail-center-btn {
    display: block;
    width: 260px;
    margin: 6px auto;
  }

  .table-title {
    font-size: 14px;
    font-weight: 400;
    line-height: 21px;
    height: 26px;
    color: #5184ec;
    text-indent: 10px;
    border-bottom: 1px solid #d5d5d5;
    background: url("../../../assets/images/line.png") no-repeat;
    background-position: -143px 22px;
  }

  .material-pic {
    background-color: #fff;
    border: 1px solid #CCDCE7;
    border-radius: 5px;
    width: 100%;
    max-width: 330px;
    height: 190px;
    margin-top: 15px;
    margin-left: 5px;
    display: flex;
    align-items: center;
    justify-content: center;

    img {
      width: 209.28px;
      height: 173.6px;
    }
  }

  .material-form {
    margin-top: 10px;

    ::v-deep .el-form-item__label {
      font-weight: normal;
    }

    ::v-deep .el-form-item {
      width: 46%;
      margin-bottom: 12px;
    }

    ::v-deep .el-input--medium {
      width: calc(100% - 70px);
    }

    ::v-deep .el-select {
      .el-input--medium {
        width: 100% !important;
      }

      width: calc(100% - 70px) !important;
    }
  }

  .label-title {
    width: 133px;
    font-size: 14px;
    font-weight: 400;
    line-height: 24px;
    height: 26px;
    color: #5184ec;
    text-indent: 10px;
    border-left: 3px solid #166BCE;
    background: url("../../../assets/images/label_bg.png") no-repeat;
    background-size: 100%;
  }


  .log-warp {
    width: 96%;
    margin-left: 2%;
    border: 1px solid #ddd;
    border-radius: 5px;
    overflow: hidden;
    padding: 5px 10px;
    height: 100%;
    font-size: 14px;
  }

  .label-log-form {
    margin-top: 5px;
    text-align: center;
  }

  .log-warp + .detail-center-btn {
    width: 300px;
  }

  .label-subscript {
    display: inline-block;
    width: 70px;
  }
  .dg-label-subscript {
    display: inline-block;
    width: 100px;
  }

  .structure-form {
    .label-subscript {
      width: 90px;
    }
  }

  .structure-form {
    margin-top: 10px;
    padding: 0 10px;

    ::v-deep .el-form-item__label {
      font-weight: normal;
    }

    ::v-deep .el-form-item {
      width: 31%;
      margin-bottom: 5px !important;
    }

    ::v-deep .el-input--medium {
      width: calc(100% - 90px);
      margin-bottom: 5px !important;
    }

    ::v-deep .el-input-group__append {
      padding: 0 6px;
    }

    ::v-deep .el-select {
      .el-input--medium {
        width: 100% !important;
      }

      width: calc(100% - 90px) !important;
    }
    ::v-deep .el-cascader {
      .el-input--medium {
        width: 100% !important;
      }

      width: calc(100% - 90px) !important;
    }

  }

  .dg-structure-form {
    margin-top: 10px;
    padding: 0 10px;

    ::v-deep .el-form-item__label {
      font-weight: normal;
    }

    ::v-deep .el-form-item {
      width: 46%;
      margin-bottom: 5px !important;
    }

    ::v-deep .el-input--medium {
      width: calc(100% - 100px);
      margin-bottom: 5px !important;
    }

    ::v-deep .el-input-group__append {
      padding: 0 6px;
    }

    ::v-deep .el-select {
      .el-input--medium {
        width: 100% !important;
      }

      width: calc(100% - 100px) !important;
    }
  }

  .structure-forms {
    margin-top: 10px;
    padding: 0 10px;

    ::v-deep .el-form-item__label {
      font-weight: normal;
    }

    ::v-deep .el-form-item {
      width: 48%;
      margin-bottom: 15px !important;
    }

    ::v-deep .fy {
      width: 31%;
    }

    /*::v-deep .fy .el-tooltip {*/
    /*  width: 90px !important;*/
    /*}*/

    /*::v-deep .fy .el-input--medium {*/
    /*  width: calc(100% - 90px)  !important;*/
    /*}*/

    ::v-deep .el-form-item__content {
      width: calc(100% - 125px);
    }

    .form-label-tooltip {
      ::v-deep .el-form-item__content {
        width: 100%;
      }

      ::v-deep .el-tooltip {
        width: 50px;
        display: inline-block;
      }

      ::v-deep .el-input--medium {
        width: calc(100% - 50px);
      }
    }
  }
  .divcontainer {
    position: relative;
  }

  .badge {
    position: absolute;
    cursor: pointer;
    top: -5px;
    right: -10px;
    width: 30px;
    height: 30px;
    background-color: #ffffff;
    color: white;
    /* border-radius: 50%; */
    text-align: center;
    line-height: 30px;
  }
  .chart-warp{
    .el-carousel__item{
      width: 80%;
      margin-left: 10%;
    }
  }
  .line-warp{
    width: 98%;
    height: 4px;
    border-top: 1px dashed #bad7ec;
    border-bottom: 1px dashed #bad7ec;
    margin: 6px 1%;
  }
  ::v-deep .el-carousel__container{
    height: 100%;
  }
  .upload-file-uploader {
    margin-bottom: 5px;
    margin-left: auto;
    margin-right: auto;
  }
  .upload-file-list .el-upload-list__item {
    border: 1px solid #e4e7ed;
    line-height: 2;
    margin-bottom: 10px;
    position: relative;
  }
  .upload-file-list .ele-upload-list__item-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: inherit;
  }
  .ele-upload-list__item-content-action .el-link {
    margin-right: 10px;
  }
  .fyzh{
    display: inline-flex;
    width: 100%;
  }
  /* CSS 类用于换行 */
  .wrap-text {
    white-space: pre-line;
  }

  /* 用于处理长单词或 URL 的换行 */
  .break-word {
    word-break: break-all;
    overflow-wrap: break-word;
  }

  ::v-deep .el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:before{
      // content: '*' !important;
      display: none
  }

</style>
